

function fmt(str){
    var args = arguments, flag = true, i = 1;
    str = str.replace(/%s/g, function () {
        var arg = args[i++];
        if (typeof arg === 'undefined') {
            flag = false;
            return '';
        }
        return arg;
    });
    return flag ? str : '';
}
function clickId(tid){
    if(id(tid).exists()){
        let target = id(tid).findOne();
        return clickTar(target);
    }
    return false;
}

function clickTar(target){
    if(target == null){
        return false;
    }

    if(target.clickable()){
        target.click();
    }else{
        let bounds = target.bounds()
        click(bounds.centerX(), bounds.centerY())
    }
    return true;
}


let TaskEngine = {

    mCurrentActivety : null,
    mUserName : "",


    CheckIn:function() {
     
            this.mCurrentActivety = currentActivity();
            if (this.mCurrentActivety =='android.widget.FrameLayout' )  {
                // 如果出签到就签到
            if ( clickTar(text('立即签到').findOnce()) ){            
                    sleep(1000)    ;
            }  
            if (text('点击签到').findOnce() != null) {
            
                className('android.view.View').find().pop().click();
            }
        }
    },
  EnteryMainScreen:function() {
        app.launchApp("微博");
        sleep(1000);
        clickTar(text('跳过').findOnce())
        sleep(1000);
     // 如果出签到就签到
    if ( clickTar(text('立即签到').findOnce()) )
    {
        
             sleep(1000)
             if (text('点击签到').findOnce() != null) {

             }
    }
    
    //  clickTar(text('签到完成，前往领取更多奖励').findOnce());
     mCurrentActivety = currentActivity();

      

     
     //TaskEngine.GetCurUserInfo();

    },

    GetCurUserInfo:() => {
        log("GetCurUserInfo")
        clickTar(desc('我').findOnce());

        mUserName= id('com.sina.weibo:id/group_header').findOnce().child(0).child(1).text();
        log(mUserName);

    },



    
    ChangeUser:() => {
        // log("ChangeUser")
        // clickTar(desc('我').findOnce());
        // sleep(1000);
        // className('android.widget.ImageView').find()[2].click();

        // sleep(1000);
        // id('com.sina.weibo:id/accountLayout').click();
        // sleep(1000);
        

        id('com.sina.weibo:id/tvAccountName').find().forEach(
            function(element) {
            text = element.text();
            log(text);
            if (text == '用户1795606504') {
                log('change to user 1795606504')
                     element.parent().parent().click();
            }
        }
        )
        
    },


    
    


    
    start: () => {
        auto()
        toastLog(fmt("设备尺寸：%s*%s \n手机型号：%s \n安卓版本：%s", device.width, device.height, device.model, device.release));
        TaskEngine.EnteryMainScreen() ;
        TaskEngine.ChangeUser();
    },
    test:() =>{
            if (clickTar(desc('消息'))) {
                clickTar(desc('消息'));
            }
    }

}

//TaskEngine.start()
TaskEngine.test();
